package com.speedricemanager.activity.home;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jbm.core.iface.IAcross;
import com.jbm.jsm.iface.UserEntity;
import com.speedricemanager.R;
import com.speedricemanager.activity.home.registerauditlist.RegisterAuditLIst;
import com.speedricemanager.net.AllNet;
import com.speedricemanager.other.base.BaseAty;
import com.speedricemanager.other.config.Config;
import com.speedricemanager.other.server.KeepService;
import com.speedricemanager.other.server.ServerModel;
import com.vondear.rxtools.RxActivityUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


public class LoginAty extends BaseAty {

    @InjectView(R.id.login_username)
    EditText loginUsername;
    @InjectView(R.id.login_password)
    EditText loginPassword;
    @InjectView(R.id.login_forgetPassword)
    TextView loginForgetPassword;
    @InjectView(R.id.login_login)
    TextView loginLogin;
    @InjectView(R.id.login_register)
    TextView loginRegister;
    @InjectView(R.id.login_yzm)
    EditText loginYzm;
    @InjectView(R.id.iv_code)
    ImageView ivCode;
    @InjectView(R.id.ln_login_yzm)
    LinearLayout lnLoginYzm;

    @InjectView(R.id.login_allowProtocel)
    CheckBox loginAllowProtocel;

    private String success = "1";
    AllNet net;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_aty);
        ButterKnife.inject(this);
         net=new AllNet(this);
        registerReceiver(receive, new IntentFilter("server"));

    }
    BroadcastReceiver receive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //0成功  1失败
            success = intent.getStringExtra("success");
            if ("0".equals(success)) {
                loadDismiss();
                //调用版本更新

            } else {
                loadDismiss();
                messageAlert("连接服务器失败");
            }
        }
    };
    @OnClick({R.id.login_forgetPassword, R.id.login_login, R.id.login_register,R.id.tv_login_allowProtocel})
    public void onClick(View view) {
        if ("1".equals(success)) {
            messageAlert("请稍等 正在连接服务器");
            return;
        }
        switch (view.getId()) {
            case R.id.login_forgetPassword:
//                Intent intent = new Intent(LoginAty.this, ForgetPasswordAty.class);
//                intent.putExtra("type", 0);
//                startActivity(intent);
                break;
            case R.id.login_login:
                if(!loginAllowProtocel.isChecked()){
                    messageAlert("请同意服务协议");
                    return;
                }
                onLoad();
                net.adminLogin(loginUsername.getText().toString(), loginPassword.getText().toString(), this);
                break;
            case R.id.login_register:
              // startActivity(RegisterAty.class);
                break;
            case R.id.tv_login_allowProtocel:
             //   startActivity(ProtocelAty.class);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        startService(new Intent(getApplicationContext(), KeepService.class));

    }

    @Override
    protected void onComplete(IAcross iAcross, String methodName) {
        //登录
        if ("adminLogin".equals(methodName)) {
            UserEntity s = (UserEntity) iAcross;
            ServerModel.getInterface().setM_local_id(s.getUserId());
            Config.userMsg=s;
            finish();
            startActivity(RegisterAuditLIst.class);

        } else if ("verifyWorkPass".equals(methodName)) {

        }else if("GetLastVersion".equals(methodName)){

        }
    }





    @Override
    protected void onDestroy() {
        unregisterReceiver(receive);
        super.onDestroy();
    }

    private static final int TIME_INTERVAL = 2000; // # milliseconds, desired time passed between two back presses.
    private long mBackPressed;

    @Override
    public void onBackPressed() {
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
            RxActivityUtils.finishAllActivity();
            super.onBackPressed();
            return;
        } else {
            Toast.makeText(getBaseContext(), "再次点击返回键退出", Toast.LENGTH_SHORT).show();
        }
        mBackPressed = System.currentTimeMillis();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 100:
                finish();
                break;
        }
    }
}
